/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m006.ui.controllers;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.vaadin.data.util.BeanItemContainer;
import ep.libs.implementations.AbstractModule;
import ep.persistence.entities.T005Patient;
import ep.persistence.entities.T006Prescription;
import java.util.Collection;
import javax.ws.rs.core.UriBuilder;

/**
 *
 * @author Spyros
 */
public class M006Controller {

    T005Patient patient;
    private WebResource service;

    public M006Controller(Object patientObject, AbstractModule module) {
        this.patient = (T005Patient) patientObject;
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        service = client.resource(UriBuilder.fromUri("http://localhost:8080/EPrescribing/rest").build());
    }

    public BeanItemContainer<T006Prescription> getFinalizedPrescDataSource() {
        Collection finalized = service.path("bl006").path("finalized").queryParam("patientId", patient.getId().toString()).
                get(new GenericType<Collection<T006Prescription>>() {
        });
        BeanItemContainer<T006Prescription> beanItemContainer =
                new BeanItemContainer<T006Prescription>(
                T006Prescription.class,
                finalized);
        return beanItemContainer;
    }

    public BeanItemContainer<T006Prescription> getPendingPrescDataSource() {
        Collection pending = service.path("bl006").path("unprescriped").queryParam("patientId", patient.getId().toString()).
                get(new GenericType<Collection<T006Prescription>>() {
        });
        BeanItemContainer<T006Prescription> beanItemContainer =
                new BeanItemContainer<T006Prescription>(
                T006Prescription.class,
                pending);
        return beanItemContainer;
    }

    public T005Patient getPatient() {
        return patient;
    }

    public Integer getPrescriptionId(Object prescObject) {
        T006Prescription prescription = (T006Prescription) prescObject;
        return prescription.getId();
    }

}
